package us.icitap.commandcenter.ui.dispatcher;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import org.vaadin.artur.icepush.ICEPush;

import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.ui.custom.CustColumnGenerator;
import us.icitap.commandcenter.ui.custom.CustTable;
import us.icitap.commandcenter.utils.HandleNotification;

import com.github.wolfie.refresher.Refresher;
import com.github.wolfie.refresher.Refresher.RefreshListener;
import com.vaadin.data.util.BeanItemContainer;


public class NewCallTable extends CustTable  implements MessageListener{
	
	private ICEPush pusher = new ICEPush();
	
	final BeanItemContainer<Call> beans = new BeanItemContainer<Call>(Call.class);
	
	public NewCallTable(boolean oneDispatcher) {
		
		this.setContainerDataSource(beans);
		
		if(!oneDispatcher){
			this.setVisibleColumns(new Object[]{"caller","recordDate", "callReason", "address", "description", "actualGrade", "mainDispatcher"});
			this.setColumnHeaders(new String[]{"TELEFONUESI", "DT. REGJ.", "ARSYEJA E TELEFONATES", "ADRESA", "PERSHKRIMI", "GRADA", "ZONA MBULIMIT"});
			this.addGeneratedColumn("mainDispatcher", CustColumnGenerator.dispatcherColumnGenerator);
		}
		else{
			this.setVisibleColumns(new Object[]{"caller","recordDate", "callReason", "address", "description", "actualGrade"});
			this.setColumnHeaders(new String[]{"TELEFONUESI", "DT. REGJ.", "ARSYEJA E TELEFONATES", "ADRESA", "PERSHKRIMI", "GRADA"});
		}
		
		this.addGeneratedColumn("callReason", CustColumnGenerator.callReasonColumnGenerator);
		this.addGeneratedColumn("actualGrade", CustColumnGenerator.gradeColumnGenerator);
		
		this.setPageLength(4);
		this.setSizeFull();
		this.setColumnWidth("callReason", 100);
		this.setColumnWidth("address", 200);
		this.setColumnWidth("description", 200);
		this.setImmediate(true);
		
		this.setSelectable(true);
		this.addExtension(pusher);
		
		this.addStyleName("red");
	}
	

	public void updateTable(){
		boolean[] order = {false};
		this.sort(new Object[]{"recordDate"}, order);
		this.setCaption("Totali i telefonatave me status TELEFONATE E RE: " + beans.size());
	}
	

	public void addCallToTable(List<Call> callList){
		beans.addAll(callList);
		updateTable();
	}
	
	public void addCallToTable(Call call){
		beans.addBean(call);
		updateTable();
	}
	
	public void removeCallFromTable(Call call){
		this.beans.removeItem(call);
		this.updateTable();
	}
	

	@Override
	public void onMessage(Message message) {
		ObjectMessage objectMessage = (ObjectMessage)message;
		try {
			
			CallMessage callMessage  = (CallMessage) objectMessage.getObject();
			this.addCallToTable(callMessage.getCall());
			HandleNotification.showInfoOnScreen("Erdhi nje telefonate e re");	
			
			pusher.push();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
}
